//
//  MovieDataModel.m
//  ShowTimeApp
//
//  Created by Ruby on 11/6/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "MovieDataModel.h"
#import "CinemaService.h"
#import "ShowtimeDataModel.h"

@implementation MovieDataModel

-(id)initMovieDataModel{
    [self initBaseDataModel];
    //[self addObjects];
    return self;
}

-(NSMutableArray*)getAllMovieSortByTitleAscending{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"TitleVI" ascending:YES selector:nil];
    // Goi ham o lop cha
    [self fetchObjects:@"Movie" sortBy:sortDescriptor];
    return (NSMutableArray*)fetchResultController.fetchedObjects;
}

-(Movie*)getDetailByID:(int)ID{
    Movie* mv = nil;
    NSString* condition=[NSString stringWithFormat:@"ID==%@",[NSNumber numberWithInt:ID]];
    NSPredicate* predicate = [NSPredicate predicateWithFormat:condition];
    mv= (Movie*)[self fetchObject:@"Movie" byPredicate:predicate];
    return mv;
}

-(void)insertObject:(id)movie{
    CinemaService_MovieEntity *newMovie =(CinemaService_MovieEntity*)movie;
    Movie *m = (Movie*)[NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:self.managedObjectContext];
    m.ID = [newMovie ID_];
    m.TitleEN=[newMovie TitleEN];
    m.TitleVI=[newMovie TitleVI];
    m.Genre = [newMovie Genre];
    m.Description=[newMovie Description];
    m.MovieStudio=[newMovie MovieStudio];
    m.Director=[newMovie Director];
    m.Writer=[newMovie Writer];
    m.Actors=[newMovie Actors];
    m.Language=[newMovie Language];
    m.Length=[newMovie Length];
    m.Version=[newMovie Version];
    m.Country=[newMovie Country];
    m.ImageUrl=[newMovie ImageUrl];
    m.TrailerUrl=[newMovie TrailerUrl];
    m.Rating=[newMovie Rating];
    m.Discount=[newMovie Discount];
    m.StartedDate=[newMovie StartedDate];
    m.EndedDate=[newMovie EndedDate];
}

-(void)insertObjects:(NSMutableArray *)movieList{
    for (id m in movieList){
        [self insertObject:m];
    }
}

+(Movie*)convertFromCinemaService_MovieEntity:(CinemaService_MovieEntity *)svMovieEntity TodbMovieEntity:(Movie *)dbMovieEntity{
    CinemaService_MovieEntity *newMovie= (CinemaService_MovieEntity *)svMovieEntity; 
    dbMovieEntity.ID = [newMovie ID_];
    dbMovieEntity.TitleEN=[newMovie TitleEN];
    dbMovieEntity.TitleVI=[newMovie TitleVI];
    dbMovieEntity.Genre = [newMovie Genre];
    dbMovieEntity.Description=[newMovie Description];
    dbMovieEntity.MovieStudio=[newMovie MovieStudio];
    dbMovieEntity.Director=[newMovie Director];
    dbMovieEntity.Writer=[newMovie Writer];
    dbMovieEntity.Actors=[newMovie Actors];
    dbMovieEntity.Language=[newMovie Language];
    dbMovieEntity.Length=[newMovie Length];
    dbMovieEntity.Version=[newMovie Version];
    dbMovieEntity.Country=[newMovie Country];
    dbMovieEntity.ImageUrl=[newMovie ImageUrl];
    dbMovieEntity.TrailerUrl=[newMovie TrailerUrl];
    dbMovieEntity.Rating=[newMovie Rating];
    dbMovieEntity.Discount=[newMovie Discount];
    return dbMovieEntity;
}

-(void)deleteAll{
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"ID" ascending:YES selector:nil];
    [self fetchObjects:@"Movie" sortBy:sortDescriptor];
    [self removeObjects];
}

-(NSMutableArray*)getAllMoviesByCinemaSortByTitleAscending:(int)cinemaID{
    NSMutableArray* result=[[NSMutableArray alloc]init];
    // Add a sort descriptor
	NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"TitleVI" ascending:YES selector:nil];
    
    // Goi ham o lop cha
    [self fetchObjects:@"Movie" sortBy:sortDescriptor];
    [self output:(NSMutableArray*)self.fetchResultController.fetchedObjects];
    ShowtimeDataModel *showtimeDataModel = [[ShowtimeDataModel alloc]initShowtimeDataModel];
    for(id mv in self.fetchResultController.fetchedObjects){
        int movieID = [((Movie*) mv).ID intValue];
        if([showtimeDataModel isMovieInShowtimeOfCinema:cinemaID withMovieID:movieID]){
            [result addObject:mv];
        }
    }
    return [result retain];    
}
-(void)output:(NSMutableArray *)movieList{
    if(movieList.count>0)
    {
        for (id movie in movieList){ 
            Movie* m = (Movie*)movie;
            NSLog(@"%@ - %@ - %@",m.ID,m.TitleVI,m.Actors);
        }
    }    
}

@end
